package net.turnbig.wechat.bot;

import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 
 * Wechat sprider runtime context
 * 
 * @author Woo Cubic
 * @date   2017-05-10 15:33:15
 */
public class WechatContext {

	String uuid;
	String baseURI;

	// fetch from login response
	String uin;
	String sid;
	String skey;
	String deviceId;
	String passTicket;

	// login user
	JSONObject self;
	JSONObject syncKey;

	List<JSONObject> newContactRequests = Lists.newArrayList();

	// 微信同步服务器
	String heartbeatUrl;

	/**
	 * common request body for current login user
	 *  
	 * @return
	 */
	public HashMap<String, Object> getBaseRequestBody() {
		HashMap<String, String> baseRequest = Maps.newHashMap();
		baseRequest.put("Uin", this.getUin());
		baseRequest.put("Sid", this.getSid());
		baseRequest.put("Skey", this.getSkey());
		baseRequest.put("DeviceID", this.getDeviceId());

		HashMap<String, Object> body = Maps.newHashMap();
		body.put("BaseRequest", baseRequest);
		return body;
	}

	/**
	 * 将同步Key转化为string格式 
	 * 
	 * @return
	 */
	public String getSyncKeyString() {
		StringBuffer sb = new StringBuffer();
		JSONArray list = getSyncKey().getJSONArray("List");
		for (int i = 0, len = list.size(); i < len; i++) {
			JSONObject key = list.getJSONObject(i);
			sb.append("|").append(key.getIntValue("Key")).append("_").append(key.getIntValue("Val"));
		}
		return sb.substring(1);
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getBaseURI() {
		return baseURI;
	}

	public void setBaseURI(String baseURI) {
		this.baseURI = baseURI;
	}

	public String getUin() {
		return uin;
	}

	public void setUin(String uin) {
		this.uin = uin;
	}

	public String getSid() {
		return sid;
	}

	public void setSid(String sid) {
		this.sid = sid;
	}

	public String getSkey() {
		return skey;
	}

	public void setSkey(String skey) {
		this.skey = skey;
	}

	public String getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(String deviceId) {
		this.deviceId = deviceId;
	}

	public String getPassTicket() {
		return passTicket;
	}

	public void setPassTicket(String passTicket) {
		this.passTicket = passTicket;
	}

	public JSONObject getSelf() {
		return self;
	}

	public void setSelf(JSONObject self) {
		this.self = self;
	}

	public JSONObject getSyncKey() {
		return syncKey;
	}

	public void setSyncKey(JSONObject syncKey) {
		this.syncKey = syncKey;
	}

	public String getHeartbeatUrl() {
		return heartbeatUrl;
	}

	public void setHeartbeatUrl(String heartbeatUrl) {
		this.heartbeatUrl = heartbeatUrl;
	}

	public List<JSONObject> getNewContactRequests() {
		return newContactRequests;
	}

	public void addNewContactRequest(JSONObject request) {
		getNewContactRequests().add(request);
	}

	@Override
	public String toString() {
		return "WechatContext [uuid=" + uuid + ", baseURI=" + baseURI + ", uin=" + uin + ", sid=" + sid + ", skey="
				+ skey + ", deviceId=" + deviceId + ", passTicket=" + passTicket + ", self=" + self + ", syncKey="
				+ syncKey + "]";
	}

}
